package se.zens.demo.wsapp.configuration;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import se.zens.common.PrefixProperties;
import se.zens.common.ZensEnvironment;
import se.zens.common.exception.ConfigurationException;


/**
 * Configuration for WSApp
 */
public class WSAppConfiguration 
{
	private Logger logger = LogManager.getLogger(this.getClass());
	
	private Set<WSConfiguration> webserviceConf ;
	
	public WSAppConfiguration(String moduleName) throws ConfigurationException
	{
		PrefixProperties moduleProperties = ZensEnvironment.getConfigProperties(moduleName);
		loadWebservices(new PrefixProperties("webservice.", moduleProperties));
		
	}
	/**
	 * Loading webservice published in this app
	 * @param moduleProperties
	 */
	private void loadWebservices(PrefixProperties wsProperties) throws ConfigurationException
	{
		webserviceConf = new HashSet< WSConfiguration>();
		int i =1;
		while (true)
		{
			String url = wsProperties.getProperty(i + ".url");
			if(url == null)
				break;
			String protocol = wsProperties.getProperty(i + ".protocol", null, true);
			String implClass = wsProperties.getProperty(i + ".implementation", null, true);
			String interfaceClass = wsProperties.getProperty(i + ".interface", null, true);
			
			webserviceConf.add(new WSConfiguration(url,WSConfiguration.WebserviceProtocol.valueOf(protocol),interfaceClass,implClass));
			i++;
		}	
		logger.info("Loaded webservices : " + webserviceConf);
	}
	public Set<WSConfiguration> getWebserviceConf()
	{
		return webserviceConf;
	}
	
	
}
